﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace HoaDon
{
	public partial class ChangeSubUserInfo : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				SucsessChangePassword.InnerText = "";

				if (!IsPostBack)
				{
					LoadInfomation();
				}
			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}

		private void LoadInfomation()
		{
			SqlConnection proxyConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString);
			proxyConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT * FROM userinfo WHERE UPPER(UserID) = @UserID", proxyConn);
			sqlComm.CommandType = CommandType.Text;
			sqlComm.Parameters.Add("@UserID", SqlDbType.VarChar).Value = Session["SubAccountID"].ToString().ToUpper();
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtUser = new DataTable();
			sqlDA.Fill(dtUser);
			string listKho = "";
			if (dtUser.Rows.Count > 0)
			{
				txtAgentName.Text = dtUser.Rows[0]["UserName"].ToString();
				txtPhone.Text = dtUser.Rows[0]["Phone"].ToString();
				txtCompany.Text = dtUser.Rows[0]["Company"].ToString();
				txtAddress.Text = dtUser.Rows[0]["Address"].ToString();
				if (dtUser.Rows[0]["ListKhoID"] != DBNull.Value)
				{
					listKho = dtUser.Rows[0]["ListKhoID"].ToString().Trim();
				}
			}
			proxyConn.Close();
			proxyConn.Dispose();

			if (CommonFunction.GetReportType("HDA") == "0" || CommonFunction.GetReportType("HDA") == "1")
			{
				rb80.Checked = true;
			}
			else
			{
				rbA5.Checked = true;
			}


			DataTable dtKho = new DataTable();
			dtKho = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT KhoID, KhoName FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");

			cklKho.DataSource = dtKho;
			cklKho.DataValueField = "KhoID";
			cklKho.DataTextField = "KhoName";
			cklKho.DataBind();

			foreach (ListItem item in cklKho.Items)
			{
				item.Selected = false;
				for (int i = 0; i < listKho.Split(',').Length; i++)
				{
					if (item.Value.ToUpper() == listKho.Split(',')[i].ToUpper())
					{
						item.Selected = true;
						break;
					}
				}
			}
		}

		protected void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				if (Session["SubAccountID"].ToString().ToUpper() == "DEMO001")
				{
					SucsessChangePassword.InnerText = "Đây là tài khoản dùng thử, không được thay đổi thông tin";
					return;
				}

				SqlConnection proxyConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString);
				proxyConn.Open();

				string sCom = "UPDATE userinfo SET ";
				if (txtPassword.Text.Trim().Length > 0)
				{
					sCom = sCom + "Password = @Password, ";
				}
				sCom = sCom + "UserName = @UserName, ";
				sCom = sCom + "Phone = @Phone, ";
				sCom = sCom + "Company = @Company, ";
				sCom = sCom + "Address = @Address, ";
				sCom = sCom + "ListKhoID = @ListKhoID ";
				sCom = sCom + "WHERE UPPER(UserID) = @UserID";

				SqlCommand sqlComm = new SqlCommand(sCom, proxyConn);
				sqlComm.CommandType = CommandType.Text;
				if (txtPassword.Text.Trim().Length > 0)
				{
					sqlComm.Parameters.Add("@Password", SqlDbType.VarChar).Value = Enc.Encrypt(txtPassword.Text.Trim(), Session["SubAccountID"].ToString().ToUpper());
				}
				sqlComm.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtAgentName.Text;
				sqlComm.Parameters.Add("@Phone", SqlDbType.VarChar).Value = txtPhone.Text;
				sqlComm.Parameters.Add("@Company", SqlDbType.NVarChar).Value = txtCompany.Text;
				sqlComm.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text;

				string sListKhoID = "";
				foreach (ListItem item in cklKho.Items)
				{
					if (item.Selected == true)
					{
						sListKhoID = sListKhoID + item.Value + ",";
					}
				}
				sqlComm.Parameters.Add("@ListKhoID", SqlDbType.VarChar).Value = sListKhoID.Substring(0, sListKhoID.Length - 1);
				sqlComm.Parameters.Add("@UserID", SqlDbType.VarChar).Value = Session["SubAccountID"].ToString().ToUpper();
				if (sqlComm.ExecuteNonQuery() > 0)
				{
					SucsessChangePassword.InnerText = "Lưu thông tin thay đổi thành công!";
				}
				proxyConn.Close();
				proxyConn.Dispose();

				if (rb80.Checked == true)
				{
					CommonFunction.SetReportType("HDA", "1");
				}
				else
				{
					CommonFunction.SetReportType("HDA", "2");
				}

			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}
	}
}
